<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">


  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="java,offer," />










<meta name="description" content="&amp;lt;&amp;lt;剑指Offer第二版&amp;gt;&amp;gt; 求职必备 算法&amp;amp;数据结构">
<meta name="keywords" content="java,offer">
<meta property="og:type" content="article">
<meta property="og:title" content="剑指Offer2">
<meta property="og:url" content="http://tomasyao.gitee.info/2018/09/04/剑指Offer2/index.html">
<meta property="og:site_name" content="Tomasyao">
<meta property="og:description" content="&amp;lt;&amp;lt;剑指Offer第二版&amp;gt;&amp;gt; 求职必备 算法&amp;amp;数据结构">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-11-30T04:02:41.140Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="剑指Offer2">
<meta name="twitter:description" content="&amp;lt;&amp;lt;剑指Offer第二版&amp;gt;&amp;gt; 求职必备 算法&amp;amp;数据结构">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"hide","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://tomasyao.gitee.info/2018/09/04/剑指Offer2/"/>





  <title>剑指Offer2 | Tomasyao</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before">
          <i style="background:rgba(204,163,81,0.8);height:2px;"></i>
        </span>
        <span class="site-title">Tomasyao</span>
        <span class="logo-line-after">
          <i style="background:rgba(204,163,81,0.8);height:2px;"></i>
        </span>
      </a>
    </div>
      
        <p class="site-subtitle">与世界分享我刚编的故事</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://tomasyao.gitee.info/2018/09/04/剑指Offer2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="tomasyao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://pic.tomasyao.info/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tomasyao">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">剑指Offer2</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-04T20:21:10+08:00">
                2018-09-04
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,639
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  6
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <div class="note success"><p><strong><em>&lt;&lt;剑指Offer第二版&gt;&gt;</em></strong> 求职必备 <code>算法&amp;数据结构</code></p></div>
<a id="more"></a>
<blockquote>
<ol>
<li>赋值运算符函数 <code>p25</code></li>
<li>实现Singleton模式 <code>p32</code></li>
<li>数组中重复的数字<br> 题目一:找出数组中重复的数字 <code>p39</code><br> 题目二:不修改数组找出重复的数字 <code>p41</code></li>
<li>二维数组中的查找 <code>p44</code></li>
<li>替换空格 <code>p51</code></li>
<li>从尾到头打印链表 <code>p58</code></li>
<li>重建二叉树 <code>p62</code></li>
<li>二叉树的下一个节点 <code>p65</code></li>
<li>用两个栈实现队列 <code>p68</code></li>
<li>斐波那契数列<br>题目一:求斐波那契数列的第n项 <code>p74</code><br>题目二:青蛙跳台阶问题 <code>p77</code></li>
<li>旋转数组的最小数字 <code>p82</code> </li>
<li>矩阵中的路径(回溯法) <code>p89</code></li>
<li>机器人的运动范围 <code>p92</code></li>
<li>剪绳子(动态规划|贪婪算法) <code>p96</code></li>
<li>二进制中1的个数 <code>p100</code></li>
<li>数值的整数次方(底数是否为0,指数是否为负数) <code>p110</code></li>
<li>打印从1到最大的n位数 <code>p114</code></li>
<li>删除链表的节点<br>题目一:在O(1)时间内删除链表节点 <code>p119</code><br>题目二:删除链表中重复的节点 <code>p122</code></li>
<li>正则表达式匹配(底层原理有点难哦) <code>p124</code></li>
<li>表示数值的字符串(底层原理有点难哦) <code>p127</code></li>
<li>调整数组顺序使奇数位于偶数前面(一次遍历,两个指针,注意解耦以增强扩展性) <code>p129</code></li>
<li>链表中倒数第k个节点(一次遍历,两个指针) <code>p134</code></li>
<li>链表中环的入口节点 <code>p139</code><br>(1.慢1步快2步验证是否存在环<br> 2.以返回的环中节点为基础得出环的长度n<br> 3.p1先走n步,p2接着同时走,相遇点即为环的入口节点) </li>
<li>反转链表(3个指针) <code>p142</code></li>
<li>合并两个排序的链表(2指针,递归) <code>p145</code></li>
<li>树的子结构 <code>p148</code></li>
<li>二叉树的镜像 <code>p157</code></li>
<li>对称的二叉树 <code>p159</code></li>
<li>顺时针打印矩阵 <code>p161</code></li>
<li>包含min函数的栈(数据栈和辅助栈) <code>p165</code></li>
<li>栈的压入,弹出序列(底层原理有点难哦) <code>p168</code></li>
<li>从上到下打印二叉树<br>题目一:不分行从上到下打印二叉树(队列,广度优先遍历)  <code>p171</code><br>题目二:分行从上到下打印二叉树(队列,加两个变量:当前层中还没有被打印的节点数,下一层中的节点数)  <code>p174</code><br>题目三:之字形打印二叉树(两个栈,分奇偶)  <code>p176</code></li>
<li>二叉搜索树的后序遍历序列(递归,左右根)  <code>p179</code></li>
<li>二叉树中和为某一值的路径(递归,栈(vector不用stack)保存路径) <code>p182</code></li>
<li>复杂链表的复制(比较复杂,有多种方法) <code>p187</code></li>
<li>二叉搜索树与双向链表(中序遍历) <code>p191</code></li>
<li>序列化和反序列化二叉树(前序遍历) <code>p195</code></li>
<li>字符串的排列(排列组合问题,递归) <code>p197</code></li>
<li>数组中出现次数超过一半的数字(解法一:快速排序核心算法partition 解法二:两个变量一个数值一个个数) <code>p205</code></li>
<li>最小的k个数(数组排序,解法一:O(n)修改数组 解法二:O(nlogk)不动原数组,特别适合处理海量数据) <code>p209</code></li>
<li>数据流中的中位数(最大堆,最小堆) <code>p214</code></li>
<li>连续子数组的最大和(找规律,或动态规划(推荐)) <code>p218</code></li>
<li>1~n整数中1出现的次数(找数字规律,很难) <code>p221</code></li>
<li>数字序列中某一位的数字(找数字规律,中等) <code>p225</code></li>
<li>把数组排成最小的数(隐形的大数问题,证明算法有效很复杂) <code>p227</code></li>
<li>把数字翻译成字符串(递归,从右到左开始翻译) <code>p231</code></li>
<li>礼物的最大价值(动态规划,用递归分析,用循环实现) <code>p233</code></li>
<li>最长不含重复字符的子字符串(动态规划,分析复杂) <code>p236</code></li>
<li>丑数(空间换时间) <code>p240</code></li>
<li>第一个只出现一次的字符<br>题目一:字符串中第一个只出现一次的字符(map) <code>p243</code><br>题目二:字符流中第一个只出现一次的字符(额) <code>p247</code></li>
<li>数组中的逆序对(归并排序) <code>p249</code></li>
<li>两个链表的第一个公共节点(Y型,先遍历两个链表得到长度,长的先走,再同时走,第一个相同的节点即为所求) <code>p253</code></li>
<li>在排序数组中查找数字(二分查找)<br>题目一:数字在排序数组中出现的次数 <code>p263</code><br>题目二:0~n-1中缺失的数字 <code>p266</code><br>题目三:数组中数值和下标相等的元素 <code>p267</code></li>
<li>二叉搜索树的第k大节点(中序遍历序列是递增序列) <code>p269</code></li>
<li>二叉树的深度<br>题目一:二叉树的深度(递归,1+左右子树的深度较大者) <code>p271</code><br>题目二:平衡二叉树 <code>p273</code></li>
<li>数组中数字出现的次数<br>题目一:数组中只出现一次的两个数字(十分复杂,异或运算) <code>p275</code><br>题目二:数组中唯一只出现一次的数字(十分复杂,位运算) <code>p278</code></li>
<li>和为s的数字<br>题目一:和为s的两个数字(首尾两个指针,往中间靠拢直到找到目标) <code>p280</code><br>题目二:和为s的连续正数序列(两个指针都向后走,不超速) <code>p282</code></li>
<li>翻转字符串<br>题目一:翻转单词顺序(两次翻转) <code>p284</code><br>题目二:左旋转字符串(借助翻转单词顺序的思路,把字符串分成两部分) <code>p286</code></li>
<li>队列的最大值<br>题目一:滑动窗口的最大值(很难理解) <code>p288</code><br>题目二:队列的最大值(参考滑动窗口) <code>p292</code></li>
<li>n个骰子的点数(很难) <code>p294</code></li>
<li>扑克牌中的顺子(快速排序,统计0的个数,统计空缺个数,含有对子则不是顺子) <code>p298</code></li>
<li>圆圈中最后剩下的数字(约瑟夫环 解法一:环形链表模拟圆圈 解法二:递归公式) <code>p300</code></li>
<li>股票的最大利润 <code>p304</code></li>
<li>求1+2+…+n(不能使用常规方法,构造函数,虚函数,函数指针,模板类型各种s操作) <code>p307</code></li>
<li>不用加减乘除做加法 <code>p310</code></li>
<li>构建乘积数组(不能用除法) <code>p312</code></li>
<li>把字符串转换成整数(动手前考虑周全,错误输入,边界值,正负号等等) <code>p318</code></li>
<li>树中两个节点的最低公共祖先 <code>p326</code></li>
</ol>
</blockquote>

      
    </div>
    
    
    
    
        <div>
        
    <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>


      </div>
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://pic.tomasyao.info/lALPACOG81QLSKnNBHDNAzw_828_1136.png_620x10000q90g.jpg" alt="tomasyao 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://pic.tomasyao.info/lADPACOG81QJkZHNBETNAtA_720_1092.jpg_620x10000q90g.jpg" alt="tomasyao 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/java/" rel="tag"><i class="fa fa-tag"></i> java</a>
          
            <a href="/tags/offer/" rel="tag"><i class="fa fa-tag"></i> offer</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/08/19/subversion/" rel="next" title="subversion">
                <i class="fa fa-chevron-left"></i> subversion
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/09/20/MechineLearning/" rel="prev" title="MechineLearning">
                MechineLearning <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="http://pic.tomasyao.info/avatar.png"
                alt="tomasyao" />
            
              <p class="site-author-name" itemprop="name">tomasyao</p>
              <p class="site-description motion-element" itemprop="description">D9lab</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">26</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">39</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.uisdc.com/" title="优设" target="_blank">优设</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.zhangxinxu.com/" title="张鑫旭" target="_blank">张鑫旭</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.alloyteam.com/nav/" title="Web前端导航" target="_blank">Web前端导航</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.36zhen.com/t?id=3448" title="前端书籍资料" target="_blank">前端书籍资料</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ife.baidu.com/" title="百度前端技术学院" target="_blank">百度前端技术学院</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://wf.uisdc.com/cn/" title="google前端开发基础" target="_blank">google前端开发基础</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tomasyao</span>

  
</div>


  <div class="powered-by">
  <i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
    本站访客数:<span id="busuanzi_value_site_uv"></span>
  </span>
  </div>



  <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>





  <span class="post-meta-divider">|</span>



  <div class="theme-info">
    <div class="powered-by"></div>
    <span class="post-count">博客全站共16.8k字</span>
  </div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/src/love.js"></script>
<script src="/live2dw/lib/L2Dwidget.min.js?0c58a1486de42ac6cc1c59c7d98ae887"></script><script>L2Dwidget.init({"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618,"jsonPath":"/live2dw/assets/tororo.model.json"},"display":{"superSample":2,"width":80,"height":100,"position":"right","hOffset":30,"vOffset":10},"mobile":{"show":true,"scale":0.5},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
